package fragment;

import android.animation.ArgbEvaluator;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.administrator.projectmoudle_two.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by eyesee on 2017/6/18.
 */

public class ArgbEvaluatorFragment extends BaseFragment {

    private View mView;
    private Toolbar mToolBar;
    private ViewPager mViewPager;

    private int ONE_COLOR;
    private int TWO_COLOR;
    private int THREE_COLOR;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView = View.inflate(mContext, R.layout.fragment_argbevaluator, null);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initColor();
        initView(mView);
        initPage();
        return mView;
    }

    private void initPage() {
        List<TextView> itemList = new ArrayList<>();
        TextView tvPageOne = new TextView(mContext);
        tvPageOne.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        tvPageOne.setGravity(Gravity.CENTER);
        tvPageOne.setText("第一页");
        tvPageOne.setBackgroundColor(ONE_COLOR);
        itemList.add(tvPageOne);
        TextView tvPageTwo = new TextView(mContext);
        tvPageTwo.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        tvPageTwo.setGravity(Gravity.CENTER);
        tvPageTwo.setText("第二页");
        tvPageTwo.setBackgroundColor(TWO_COLOR);
        itemList.add(tvPageTwo);
        TextView tvPageThree = new TextView(mContext);
        tvPageThree.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        tvPageThree.setGravity(Gravity.CENTER);
        tvPageThree.setText("第三页");
        tvPageThree.setBackgroundColor(THREE_COLOR);
        itemList.add(tvPageThree);
        mViewPager.setAdapter(new ContentPagerAdapter(itemList));
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                changeTitleColor(position,positionOffset);
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initColor() {
        ONE_COLOR = ContextCompat.getColor(mContext, R.color.colorPrimary);
        TWO_COLOR = ContextCompat.getColor(mContext,R.color.colorAccent);
        THREE_COLOR = ContextCompat.getColor(mContext,R.color.colorPrimaryDark);
    }

    private void initView(View view) {
        mToolBar = ((Toolbar) view.findViewById(R.id.toolbar));
        mViewPager = ((ViewPager) view.findViewById(R.id.viewpager));
    }



    private void changeTitleColor(int position, float positionOffset) {
        switch (position) {
            case 0:
                ArgbEvaluator argbEvaluator = new ArgbEvaluator();//渐变色计算类
                int currentLastColor = (int) (argbEvaluator.evaluate(positionOffset, ONE_COLOR, TWO_COLOR));
                //positionOffset:表示渐变度，取0.0F-1.0F之间某一值
                //PAGE_COLOR_ONE:表示起始颜色值
                //PAGE_COLOR_TWO:表示最终颜色值
                setToolbarColor(currentLastColor);
                setStatusBarColor(currentLastColor);
                break;
            case 1:
                ArgbEvaluator argbEvaluator1 = new ArgbEvaluator();
                int currentLastColor1 = (int) (argbEvaluator1.evaluate(positionOffset, TWO_COLOR, THREE_COLOR));
                setToolbarColor(currentLastColor1);
                setStatusBarColor(currentLastColor1);
                break;
            case 2:
                setStatusBarColor(THREE_COLOR);
                break;
        }
    }

    private void setStatusBarColor(int color) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = mContext.getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(color);
                //window.setNavigationBarColor(activity.getResources().getColor(colorResId));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setToolbarColor(int color) {
        if (mToolBar != null) {
            mToolBar.setBackgroundColor(color);
        }
    }

    private class ContentPagerAdapter extends PagerAdapter {
        private List<TextView> mItemList;

        public ContentPagerAdapter(List<TextView> itemList) {
            this.mItemList = itemList;
        }

        @Override
        public int getCount() {
            return mItemList==null?0:mItemList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mItemList.get(position));
            return mItemList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mItemList.get(position));
        }
    }
}
